package com.irdstudio.efp.esb.service.impl.hj;

import com.alibaba.fastjson.JSONObject;
import com.irdstudio.basic.framework.core.constant.ESBConstant;
import com.irdstudio.basic.framework.core.exception.ESBException;
import com.irdstudio.efp.esb.common.client.resp.EsbRespServiceBean;
import com.irdstudio.efp.esb.common.constant.ESBClientConstance;
import com.irdstudio.efp.esb.common.constant.EsbBizEnums;
import com.irdstudio.efp.esb.service.bo.req.hj.RepayInfoQueryReqBean;
import com.irdstudio.efp.esb.service.bo.req.hj.RepayInfoQueryTotalReqBean;
import com.irdstudio.efp.esb.service.bo.resp.hj.RepayInfoQueryRespBean;
import com.irdstudio.efp.esb.service.bo.resp.hj.RepayInfoQueryTotalRespBean;
import com.irdstudio.efp.esb.service.client.ESBRequestBuilder;
import com.irdstudio.efp.esb.service.client.EsbBaseBean;
import com.irdstudio.efp.esb.service.facade.hj.RepayInfoQueryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service("repayInfoQueryService")
public class RepayInfoQueryServiceImpl implements RepayInfoQueryService {
    private static Logger logger = LoggerFactory.getLogger(RepayInfoQueryServiceImpl.class);

    @Override
    public RepayInfoQueryRespBean applyRepayInfoQuery(RepayInfoQueryReqBean repayInfoQueryReqBean) throws ESBException {
        logger.info("======>回收信息查询【" + repayInfoQueryReqBean.getGlobaNo() + "_30230002_03】开始<======");
        RepayInfoQueryRespBean repayInfoQueryRespBean = null;
        try {
            EsbBaseBean esbBaseBean = EsbBaseBean.EsbBaseBeanBuilder.
                    anEsbBaseBean().
                    withINSTID(ESBClientConstance.bdInstId).withGLBLSRLNO(repayInfoQueryReqBean.getGlobaNo()).build();
            EsbRespServiceBean recvBean = ESBRequestBuilder.buildClient().
                    withEsbBaseBean(esbBaseBean)
                    .withBody(repayInfoQueryReqBean)
                    .withTradeNo(ESBConstant.TRADE_CODE_30230002)
                    .withScene(ESBConstant.SCENE_NO_03)
                    .create()
                    .sendAndReceive();
            if (EsbBizEnums.RetCodeEnum.SUCCESS.VALUE.equals(recvBean.getSysHead().getRetInfArry().get(0).getRetCd())) {
                repayInfoQueryRespBean = recvBean.getBody(RepayInfoQueryRespBean.class);
            } else if ("NL9202".equals(recvBean.getSysHead().getRetInfArry().get(0).getRetCd()) || "NL9203".equals(recvBean.getSysHead().getRetInfArry().get(0).getRetCd())) {
                //"NL9202","RetMsg":"NL9202 回收信息不存在!"}
                repayInfoQueryRespBean = new RepayInfoQueryRespBean();
                repayInfoQueryRespBean.setRpyDtlInfArrys(new ArrayList<>());
            } else {
                throw new ESBException(recvBean.getSysHead().getRetInfArry().get(0).getRetMsg());
            }
            logger.info("回收信息查询【30230002_03】结束，返回信息：" + JSONObject.toJSONString(repayInfoQueryRespBean));
        } catch (Exception e) {
            logger.error("回收信息查询【30230002_03】出现异常：" + e.getMessage());
            throw new ESBException("回收信息查询【30230002_03】出现异常：" + e.getMessage());
        } finally {
            logger.info("======>回收信息查询【" + repayInfoQueryReqBean.getGlobaNo() + "_30230002_03】结束<======");
        }
        return repayInfoQueryRespBean;
    }

    @Override
    public RepayInfoQueryTotalRespBean applyRepayInfoQueryTotal(RepayInfoQueryTotalReqBean reqBean) throws ESBException {
        logger.info("======>回收信息查询【" + reqBean.getGlobaNo() + "_30230002_14】开始<======");
        RepayInfoQueryTotalRespBean respBean = null;
        try {
            EsbBaseBean esbBaseBean = EsbBaseBean.EsbBaseBeanBuilder.
                    anEsbBaseBean().
                    withINSTID(ESBClientConstance.bdInstId).withGLBLSRLNO(reqBean.getGlobaNo()).build();
            EsbRespServiceBean recvBean = ESBRequestBuilder.buildClient().
                    withEsbBaseBean(esbBaseBean)
                    .withBody(reqBean)
                    .withTradeNo(ESBConstant.TRADE_CODE_30230002)
                    .withScene(ESBConstant.SCENE_NO_14)
                    .create()
                    .sendAndReceive();
            if (EsbBizEnums.RetCodeEnum.SUCCESS.VALUE.equals(recvBean.getSysHead().getRetInfArry().get(0).getRetCd())) {
                respBean = recvBean.getBody(RepayInfoQueryTotalRespBean.class);
            } else if ("NL9202".equals(recvBean.getSysHead().getRetInfArry().get(0).getRetCd()) || "NL9203".equals(recvBean.getSysHead().getRetInfArry().get(0).getRetCd())) {
                //"NL9202","RetMsg":"NL9202 回收信息不存在!"}
                respBean = new RepayInfoQueryTotalRespBean();
                respBean.setHjLoanArrys(new ArrayList<>());
            } else {
                throw new ESBException(recvBean.getSysHead().getRetInfArry().get(0).getRetMsg());
            }
            logger.info("回收信息查询【30230002_14】结束，返回信息：" + JSONObject.toJSONString(respBean));
        } catch (Exception e) {
            logger.error("回收信息查询【30230002_14】出现异常：" + e.getMessage());
            throw new ESBException("回收信息查询【30230002_14】出现异常：" + e.getMessage());
        } finally {
            logger.info("======>回收信息查询【" + reqBean.getGlobaNo() + "_30230002_14】结束<======");
        }
        return respBean;
    }
}